package com.xmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xmall.bean.bo.StorageListBo;
import com.xmall.bean.table.Storage;
import com.xmall.bean.table.StorageExample;
import com.xmall.bean.vo.ListDataVo;
import com.xmall.mapper.StorageMapper;
import com.xmall.service.StorageService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class StorageServiceImpl implements StorageService {

    @Autowired
    StorageMapper storageMapper;

    @Override
    public ListDataVo<Storage> queryStorageList(StorageListBo storageListBo) {

        PageHelper.startPage(storageListBo.getPage(), storageListBo.getLimit());
        StorageExample storageExample = new StorageExample();
        storageExample.setOrderByClause(storageListBo.getSort() + " " + storageListBo.getOrder());
        StorageExample.Criteria criteria = storageExample.createCriteria();
        if (storageListBo.getName() != null) {
            criteria.andNameLike("%" + storageListBo.getName() + "%");
        }
        if (storageListBo.getKey() != null) {
            criteria.andKeyLike("%" + storageListBo.getKey() + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<Storage> storages = storageMapper.selectByExample(storageExample);
        PageInfo<Storage> storagePageInfo = new PageInfo<>(storages);
        int total = (int) storagePageInfo.getTotal();
        return new ListDataVo<Storage>(storages, total);
    }

    @Value("${file.path}")
    String filePath;

    @Value("${file.url}")
    String fileUrl;

    @SneakyThrows
    @Override
    public Storage createStorage(MultipartFile file) {
        int size = (int) file.getSize();
        String name = file.getName();
        String type = file.getContentType();
        String originalFilename = file.getOriginalFilename();
        String[] split = originalFilename.split("\\.");
        UUID uuid = UUID.randomUUID();
        String s = uuid.toString().replace("-", "");
        String key = s + "." + split[split.length - 1];
        File targetFile = new File(filePath, key);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        file.transferTo(targetFile);
        String url = fileUrl + key;
        Storage storage = new Storage(null, key, name, type, size, url, new Date(), new Date(), false);
        storageMapper.insert(storage);
        return storage;
    }

    @Override
    public void deleteStorage(Storage storage) {
        storage.setDeleted(true);
        storageMapper.updateByPrimaryKey(storage);
    }

    @Override
    public void updateStorage(Storage storage) {
        storageMapper.updateByPrimaryKey(storage);
    }
}
